/**
* <p>Title: RocketMQQueryUtils.java</p>
* <p>Description: </p>
* <p>Company: www.huoyunren.com</p>
* @author duanxiangyun
* @date 2016年11月24日
* @version 1.0
*/
package com.hxbd.test.utils;

import java.util.List;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.alibaba.rocketmq.remoting.exception.RemotingException;

public class RocketMQQueryUtils {

	/**
     * 通过topic、key从队列获取MQ消息
     * @param nameAddr nameserver地址
     * @param topic
     * @param key
     * @param count 获取条数（最大64条）
     * @param starttime 开始时间
     * @param endtime 结束时间
     * @return 消息列表
     */
	public static List<String> QueryMsgByKeySubCommand (String nameAddr,String topic,String key, int count, long starttime, long endtime) {
		RocketMQService mqservice = new RocketMQService(Thread.currentThread().getName()+System.currentTimeMillis(),nameAddr);
		List<String> l = mqservice.getMQMsg(topic, key, count, starttime, endtime);
		mqservice.close();
		return l;
	}
	/**
     * 通过topic、key从队列获取MQ消息
     * @param nameAddr nameserver地址
     * @param topic
     * @param key
     * @param count 获取条数（最大64条）
     * @param starttime 开始时间
     * @param endtime 结束时间
     * @return 消息列表
     */
	public static List<MessageExt> QueryMessageExtByKeySubCommand (String nameAddr,String topic,String key, int count, long starttime, long endtime) {
		RocketMQService mqservice = new RocketMQService(Thread.currentThread().getName()+System.currentTimeMillis(),nameAddr);
		List<MessageExt> l = mqservice.getMessageExt(topic, key, count, starttime, endtime);
		mqservice.close();
		return l;
	}
	
	public static List<String> QueryMsgByKeySubCommand (String nameAddr,String topic,String key, int count) {
		RocketMQService mqservice = new RocketMQService(Thread.currentThread().getName()+System.currentTimeMillis(),nameAddr);
		List<String> l = mqservice.getMQMsg(topic, key, count);
		mqservice.close();
		return l;
	}
	
	public static long getLastestStorageTime (String nameAddr,String topic,String key) {
		RocketMQService mqservice = new RocketMQService(Thread.currentThread().getName()+System.currentTimeMillis(),nameAddr);
		long time  = mqservice.getLastestTime(topic, key);
		mqservice.close();
		return time;
	}
	
	public static void deleteSubscriptionGroup (String nameAddr,String clusterName, String consumerGroup) throws InterruptedException, MQBrokerException, RemotingException, MQClientException {
		RocketMQService mqservice = new RocketMQService(Thread.currentThread().getName()+System.currentTimeMillis(),nameAddr);
		mqservice.deleteSubscriptionGroup(clusterName, consumerGroup);
		mqservice.close();
	}
}
